package com.fstec.ks.master.action;

import java.util.Calendar;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.UrlPathHelper;

import com.fstec.ks.master.RequestHelper;
import com.fstec.ks.master.bean.LoginUserBean;
import com.sinotn.SinotnLogger;
import com.sinotn.error.MessageException;
import com.sinotn.ks.thirdsys.api.uc.UserCenter;
import com.sinotn.web.RequestUtil;

@Controller
public class IndexAction {
	@Autowired
	private UserCenter userCenter;
	/*
	 * 用户中心默认监考员账号
	 */
	private String defaultUcAccount="jky";
	
	/**
	 * 模板页面跳转
	 */
	@RequestMapping(value = "/forward/**/*.*", method = RequestMethod.GET)
	public String forward(HttpServletRequest request) {
		UrlPathHelper helper = new UrlPathHelper();
		String lookuppath=helper.getLookupPathForRequest(request);
		int lastIdx=lookuppath.lastIndexOf(".");
		String forward=lookuppath.substring(9, lastIdx);
		return forward;
	}
	/**
	 * 权限过滤模板页面跳转
	 */
	@RequestMapping(value = "/R/forward/**/*.*", method = RequestMethod.GET)
	public String forwardR(HttpServletRequest request) {
		UrlPathHelper helper = new UrlPathHelper();
		String lookuppath=helper.getLookupPathForRequest(request);
		int lastIdx=lookuppath.lastIndexOf(".");
		String forward=lookuppath.substring(11, lastIdx);
		return forward;
	}
	
	/**
	 * 首页跳转
	 * @param request
	 * @return
	 * @auth <a href="mailto:xieqj@sinotn.com">谢启进</a>
	 * @date 2016年10月8日 下午12:06:05
	 */
	@RequestMapping(value = "/index.jspx")
	public String index(HttpServletRequest request){
		return "login";
	}
	/**
	 * 通过签到手机号或监考员密码登录考试控制台
	 */
	@RequestMapping(value = "/login.jspx")
	public String login(HttpServletRequest request,ModelMap model){
		try{
			Calendar calendar=Calendar.getInstance();
			int rawOffsetMinute=calendar.getTimeZone().getRawOffset()/60000;
			if(rawOffsetMinute!=480){//北京时区
				throw new MessageException("服务器时区不正确，请调整操作系统时区为北京东8区"+rawOffsetMinute);
			}
			String passwd=request.getParameter("passwd");
			if(passwd==null||passwd.length()==0){
				throw new MessageException("请输入登录密码或签到手机号");
			}
			//连接用户中心校验监考员账号密码
			if(this.userCenter.valid(this.defaultUcAccount,passwd)==false){
				throw new MessageException("密码校验不通过");
			}
			LoginUserBean bean=new LoginUserBean();
			bean.setAccount(this.defaultUcAccount);
			RequestHelper.setLoginUser(request, bean);

			return "home";
		}catch(MessageException e){
			HashMap<String, String> error=new HashMap<String, String>();
			error.put("code", "500");
			error.put("msg", e.getMessage());
			model.put("Error", error);
		}catch(Throwable e){
			SinotnLogger.DEBUG.error("登录发生系统错误@"+RequestUtil.getIpAddr(request),e);
			HashMap<String, String> error=new HashMap<String, String>();
			error.put("code", "500");
			error.put("msg", "登录发生系统错误");
			model.put("Error", error);
		}
		return "login";
	}

	/**
	 * 注销退出登录信息
	 */
	@RequestMapping(value = "/logoff.jspx")
	public String logoff(HttpServletRequest request){
		HttpSession httpSession=request.getSession();
		if(null!=httpSession){
			httpSession.invalidate();
		}
		return "login";
	}
}
